package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.UsersMapper;
import org.example.service.IUsersService;
import org.example.utils.SmsSendingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import org.example.users.pojo.Users;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-17
 */
@Service
@Slf4j
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private SmsSendingUtils smsSendingUtils;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Override
    public String loginUser(String phone) {
//        获取token
        String token = generateToken();
        if (phone.equals("")){
            return null;
        }
        LambdaQueryWrapper<Users> usersqw = new LambdaQueryWrapper<>();
        usersqw.eq(Users::getPhone,phone);
        Users users = usersMapper.selectOne(usersqw);
        if (users == null){
            log.info("用户不存在,注册一个新的用户信息");
            Users users1 = new Users();
            users1.setPhone(phone);
            users1.setNickname("默认用户名"+phone);
            users1.setSignature("这个用户很懒还未填写个性签名");
            users1.setAvatar("xxxx");
            users1.setEnergyValue(0);
            users1.setFollowCount(0);
            users1.setFollowerCount(0);
            users1.setLikeReceivedCount(0);
            users1.setIsUserAgree(1);
            users1.setCreatedTime(new Date());
            usersMapper.insert(users1);
            Users users2 = usersMapper.selectOne(usersqw);
            users2.setEnergyValue(users.getEnergyValue()+1);
            usersMapper.updateById(users2);
            redisTemplate.opsForValue().set(token,users2.getUserId().toString(),90, TimeUnit.MINUTES);
            return token;
        }else {
            users.setEnergyValue(users.getEnergyValue()+1);
            usersMapper.updateById(users);
            redisTemplate.opsForValue().set(token,users.getUserId().toString(),90, TimeUnit.MINUTES);
            return token;
        }
    }

    @Override
    public String getVerifyCode(String phone) {
        smsSendingUtils.sendCode(phone);
        return "success";
    }

    @Override
    public Boolean getToken(String token) {
        String userid = redisTemplate.opsForValue().get(token);
        Users users = usersMapper.selectById(userid);
        if (userid != null){
            return true;
        }else {
            return false;
        }
    }
//    https://gitee.com/taotaozero/ai_app.git
    public static String generateToken() {
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder token = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 13; i++) {
            int index = random.nextInt(characters.length());
            token.append(characters.charAt(index));
        }
        return token.toString();
    }
}
